package icu.xuyijie.springbootlearning2.chapter3.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author 徐一杰
 * @date 2025/02/27 15:37
 * @description 用于redis测试
 */
@RestController
@RequestMapping("/redis")
public class RedisController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/setString")
    public void setString() {
        // 向redis插入String数据
        stringRedisTemplate.opsForValue().set("string1", "阿瓦倒萨");
    }

    @GetMapping("/getString")
    public void getString() {
        String str = stringRedisTemplate.opsForValue().get("string1");
        System.out.println(str);
    }

    @GetMapping("/setList")
    public void setList() {
        stringRedisTemplate.opsForList().leftPush("list1", "1");
        stringRedisTemplate.opsForList().leftPush("list1", "2");
        stringRedisTemplate.opsForList().leftPushAll("list1", "a", "b", "c");
    }

    @GetMapping("/getList")
    public void getList() {
        List<String> list = stringRedisTemplate.opsForList().range("list1", 0, -1);
        System.out.println(list);
    }

    @GetMapping("/setExpireKey")
    public void setExpireKey() {
        stringRedisTemplate.opsForValue().set("string2", "1111");
        // 设置过期时间
        stringRedisTemplate.expire("string2", 10, TimeUnit.SECONDS);
    }

}
